//
// CommunityFunctionViewController.m
// homeland
//
// Created by caesar on 14-7-23.
// Copyright (c) 2014年 xzysun. All rights reserved.
//
#import "CommunityFunctionViewController.h"
#import "UserInfoService.h"
#import "RegisterViewController.h"
#import "CommunityUserRelation.h"
#import "CommunityRegisterControllerViewController.h"
#import "CommonUtil.h"

@interface CommunityFunctionViewController (){
    //用于区别alert
    /**海珠家园注册**/
    int  ALERT_TYPE_REGIST;
    /**社区用户注册**/
    int  ALERT_TYPE_COMMUNITY_REGIST;
}

@end
@implementation CommunityFunctionViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"IconPages" ofType:@"plist"];
    _iconPageConfigs = [[NSDictionary alloc]initWithContentsOfFile:plistPath];
    
    ALERT_TYPE_REGIST = 0;
    ALERT_TYPE_COMMUNITY_REGIST = 1;
    // Do any additional setup after loading the view from its nib.
    //不隐藏导航条
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.title = @"注册审核,您可以参与以下功能";
    //添加后退按扭和事件
    self.navigationItem.leftBarButtonItem = autoavBackButton(self, @selector(backMethod));
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"_navBar"] forBarMetrics:UIBarMetricsDefault];
    int height = iPhone5 ? 0 : 88 ;
    //生成底部按扭
    UIButton *joinInBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 460 - height, 320, 44)];
    [joinInBtn setBackgroundImage:[UIImage imageNamed:@"btnImage.png"] forState:0];
    [joinInBtn setTitle:@"我要参与" forState:0];
    [joinInBtn addTarget:self action:@selector(joinRegist) forControlEvents:64];
    [self.view addSubview:joinInBtn];
    UIImage* image = [UIImage imageNamed:@"社区自治-我要参与2.jpg"];
    UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT + 150 +height)];
    [imageView setImage:image];
    // self.backScrollView.contentSize = CGSizeMake(SCREEN_WIDTH, 504.0);
    // self.backScrollView.contentSize = imageView.frame.size;
    int imageWidth = imageView.frame.size.width;
    int imageHeight = imageView.frame.size.height;
    
    
    self.myScrollView.contentSize = CGSizeMake(imageWidth, imageHeight+70);
    [self.myScrollView setScrollEnabled:YES];
    [self.myScrollView addSubview:imageView];
}
- (void)joinRegist
{
    //用户未登录，提示注册
    if (![UserInfoService getUserInfoService].currentLoginUserName) {
        UIAlertView * alertview = [[UIAlertView alloc]initWithTitle:@"提示" message:@"该功能注册用户才能使用。现在注册" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        alertview.tag = 0;
        [alertview show];
        return;
    }else{
        //请求后台。用户是否注册了社区用户
        [self isRegistCommunity];
    }
}
//提示注册事件
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == ALERT_TYPE_REGIST) {
        //buttonIndex  == 0 是取肖，1 是确定
        if (buttonIndex == 1) {
            //跳转到注册页
            RegisterViewController *registerVC = [[RegisterViewController alloc]initWithNibName:@"RegisterViewController" bundle:nil];
            [self.navigationController pushViewController:registerVC animated:YES];
        }
    }else if (alertView.tag == ALERT_TYPE_COMMUNITY_REGIST){
        
    }
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//查看用户是否以注册社区用户
- (void)isRegistCommunity
{
    [self startLoadingViewWithText:@"正在载入..."];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    UserInfoService *userInfo=[UserInfoService getUserInfoService];
    if ([params objectForKey:@"userId"]==nil) {
        [params setValue:userInfo.userInfo.Id forKey:@"userId"];
    }
    MKNetworkEngine *currentEngine = [[MKNetworkEngine alloc] initWithHostName:SERVER_URL_COMMUNITY_IS_REGISTER customHeaderFields:nil];
    MKNetworkOperation *op = [currentEngine operationWithPath:@"" params:params httpMethod:@"POST"];
    [op addCompletionHandler:^(MKNetworkOperation *completedOperation) {
        //关闭提示加载框
        [self stopLoadingView];
        
        NSDictionary *json = [completedOperation responseJSON];
        BOOL isRegist = [[json objectForKey:@"isRegister"] boolValue];
        if (isRegist) {//
            int state = [[json objectForKey:@"state"]intValue];
            if (state == 1) {//0：正在审核。1：审核通过。2：审核不通过 ,
                NSDictionary *bean = [json objectForKey:@"communityUserRelation"];
                //字典转实体
                CommunityUserRelation *cur = getObjectFromDic([CommunityUserRelation class],bean);
                IconPagesViewController *iconPageVC = [[IconPagesViewController alloc]initWithNibName:@"IconPagesViewController" bundle:nil];
                
                NSMutableDictionary *mConfig =[NSMutableDictionary dictionaryWithDictionary:[_iconPageConfigs objectForKey:@"社区自治"]];
                
                [mConfig setValue:cur.communityName forKeyPath:@"title"];
                
                [mConfig setValue:@"社区自治" forKeyPath:@"keyTitle"];
                [mConfig setValue:[NSString stringWithFormat:@"%@",[CommonUtil dealNullObject:cur.streetId]] forKeyPath:@"streetId"];
                [mConfig setValue:[NSString stringWithFormat:@"%@", [CommonUtil dealNullObject:cur.communityId]] forKeyPath:@"communityId"];
                [mConfig setValue:[NSString stringWithFormat:@"%@",[CommonUtil dealNullObject:cur.communityName]] forKeyPath:@"communityName"];
                [mConfig setValue:[NSString stringWithFormat:@"%@",[CommonUtil dealNullObject:cur.partyMember]] forKeyPath:@"partyMember"];
                [mConfig setObject:[CommonUtil dealNullObject:cur.streetId] forKey:@"streetId"];
                [mConfig setObject:[CommonUtil dealNullObject:cur.waiterId] forKey:@"waiterId"];
                iconPageVC.config =mConfig;
                iconPageVC.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:iconPageVC animated:YES];
                
            }else if(state == 2){//审核不通过->社区注册
                CommunityRegisterControllerViewController *crVC =[[CommunityRegisterControllerViewController alloc]initWithNibName:@"CommunityRegisterControllerViewController" bundle:nil];
                crVC.hidesBottomBarWhenPushed = YES;//隐藏底部导航
                [self.navigationController pushViewController:crVC animated:YES];
                
            }else{//正在审核 提示
                NSString *message = [json objectForKey:@"message"];
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
            }
            
        }else{//如果没有注册,跳到 “社区注册参加页”
            CommunityRegisterControllerViewController *pcVC =[[CommunityRegisterControllerViewController alloc]initWithNibName:@"CommunityRegisterControllerViewController" bundle:nil]; 
            pcVC.hidesBottomBarWhenPushed = YES;//隐藏底部导航
            [self.navigationController pushViewController:pcVC animated:YES];
        }
        
        
    } errorHandler:^(MKNetworkOperation *completedOperation, NSError *error) {
        [self stopLoadingView];
        [self.view makeToast:@"请求失败" duration:2.0 position:@"center"];
        
    }];
    [currentEngine enqueueOperation:op];
}
-(void)showIconPagesWithName:(NSString*)name
{
    
}
@end